from django.db import models

# Create your models here.



# 问候表
class Greetings(models.Model):
    content = models.CharField('问候语',max_length=70)
    start_time = models.TimeField('开始时间')
    end_time = models.TimeField('结束时间')

    class Meta:
        db_table = 'tb_greetings'

    def __str__(self):
        return self.content



# 角色表
class RoleModel(models.Model):
    r_name = models.CharField('角色名', max_length=32)
    comment = models.CharField('角色备注', max_length=320)

    class Meta:
        db_table = 'tb_role'

    def __str__(self):
        return self.r_name


# 菜单表
class MenuModel(models.Model):
    m_name = models.CharField('菜单名称', max_length=10)
    m_route = models.CharField('菜单路径', max_length=50,null=True)
    icon = models.CharField("菜单图标", max_length=50,null=True)
    m_typ = models.CharField('菜单类型', max_length=32)
    assoc = models.ForeignKey("self", verbose_name='自关联', on_delete=models.CASCADE, null=True)

    class Meta:
        db_table = 'tb_menu'

    def __str__(self):
        return self.m_name


# 角色菜单表
class RoleMenuModel(models.Model):
    role = models.ForeignKey(RoleModel, on_delete=models.CASCADE, verbose_name='角色')
    menu = models.ForeignKey(MenuModel, on_delete=models.CASCADE, verbose_name='菜单')

    class Meta:
        db_table = 'tb_role_menu'

    def __str__(self):
        return self.role


# 用户表
class UserModel(models.Model):
    username = models.CharField('员工昵称', max_length=52,null=True,default=None)
    account = models.CharField('账号', max_length=228)
    password = models.CharField('密码', max_length=520)
    phone = models.CharField('手机号', max_length=11,null=True,default=None)
    address = models.CharField('地址', max_length=32,null=True,default=None)
    age = models.IntegerField('年龄')
    img = models.CharField('头像',max_length=520,null=True,default=None)
    gender = models.IntegerField('1:男,2:女',default=1)
    role = models.ForeignKey(RoleModel,verbose_name='所属角色',on_delete=models.CASCADE)

    class Meta:
        db_table = 'tb_user'

    def __str__(self):
        return self.username




# 学校表
class SchoolModel(models.Model):
    s_name = models.CharField('学校名称', max_length=52)
    user = models.ForeignKey(UserModel,verbose_name='校长',on_delete=models.CASCADE)
    class Meta:
        db_table = 'tb_school'

    def __str__(self):
        return self.s_name


# 教师表
class TeachersModel(models.Model):
    user = models.ForeignKey(UserModel,verbose_name='老师',on_delete=models.CASCADE)
    school = models.ForeignKey(SchoolModel, verbose_name='所属学校', on_delete=models.CASCADE)

    class Meta:
        db_table = 'tb_teachers'

    def __str__(self):
        return self.user


# 班级表
class ClassModel(models.Model):
    c_name = models.CharField('班级名称', max_length=52)
    teacher = models.ForeignKey(UserModel,verbose_name='所属老师',on_delete=models.CASCADE)
    school = models.ForeignKey(SchoolModel,verbose_name='所属学校',on_delete=models.CASCADE)

    class Meta:
        db_table = 'tb_class'

    def __str__(self):
        return self.c_name




 # 学生表
class StudentsModel(models.Model):
    user = models.ForeignKey(UserModel, verbose_name='学生', on_delete=models.CASCADE)
    cla = models.ForeignKey(ClassModel, verbose_name='所属班级', on_delete=models.CASCADE)

    class Meta:
        db_table = 'tb_students'

    def __str__(self):
        return self.user


# 分类
class Sort(models.Model):
    s_name = models.CharField('分类名称',max_length=100)

    class Meta:
        db_table = 'tb_sort'

    def __str__(self):
        return self.s_name


# 标签
class Label(models.Model):
    l_name = models.CharField('标签名',max_length=100)

    class Meta:
        db_table = 'tb_label'

    def __str__(self):
        return self.l_name


# 学习内容
class Learning_content(models.Model):
    title = models.CharField('标题',max_length=100)
    c_name = models.CharField('学习内容',max_length=10000)
    introduction = models.CharField('简介',max_length=100)
    user = models.ForeignKey(UserModel,verbose_name='作者',on_delete=models.CASCADE,default=None)
    is_open = models.BooleanField('是否公开',default=True)

    class Meta:
        db_table = 'tb_learning'

    def __str__(self):
        return self.title


# 评论
class Comment(models.Model):
    user = models.ForeignKey(UserModel, on_delete=models.CASCADE, verbose_name="评论人")
    common_content = models.TextField("评论内容", max_length=400,null=True,default=True)
    like_count = models.IntegerField("点赞次数", default=0, null=True, blank=True)
    parent_id = models.ForeignKey("self", on_delete=models.CASCADE, verbose_name="被回复的id",null=True,default=None)
    is_reply = models.BooleanField(verbose_name="是否回复", null=True)
    content = models.ForeignKey(Learning_content, on_delete=models.CASCADE, verbose_name="学习内容",null=True)

    reply_count = models.IntegerField("回复数", default=0, null=True, blank=True)

    class Meta:
        db_table = 'tb_comment'

    def __str__(self):
        return self.content



# 评论和学习内容中间表
class Learning_Comment(models.Model):
    learning = models.ForeignKey(Learning_content,verbose_name='学习内容id',on_delete=models.CASCADE)
    comment = models.ForeignKey(Comment,verbose_name='评论id',on_delete=models.CASCADE)

    class Meta:
        db_table = 'tb_learning_comment'

    def __str__(self):
        return self.learning

# 分类和学习内容中间表
class Learning_Sort(models.Model):
    learning = models.ForeignKey(Learning_content,verbose_name='学习内容id',on_delete=models.CASCADE)
    sort = models.ForeignKey(Sort,verbose_name='分类id',on_delete=models.CASCADE)

    class Meta:
        db_table = 'tb_learning_sort'

    def __str__(self):
        return self.learning



# 标签和学习内容中间表
class Learning_Label(models.Model):
    learning = models.ForeignKey(Learning_content,verbose_name='学习内容id',on_delete=models.CASCADE)
    label = models.ForeignKey(Label,verbose_name='标签id',on_delete=models.CASCADE)

    class Meta:
        db_table = 'tb_learning_label'

    def __str__(self):
        return self.learning




# 学习资源
class Resource(models.Model):
    r_name = models.CharField('资源名称',max_length=50)
    describe = models.CharField('资源描述',max_length=256)
    price = models.DecimalField('价格',max_digits=6,decimal_places=2)
    user = models.ForeignKey(UserModel,verbose_name='作者',on_delete=models.CASCADE)
    img = models.CharField('图片地址',max_length=365,null=True,default=None)

    class Meta:
        db_table = 'tb_resource'

    def __str__(self):
        return self.r_name


# 章节
class ChaptersModel(models.Model):
    title = models.CharField("章节名", max_length=32)
    # sequence = models.SmallIntegerField("展示顺序")
    resource = models.ForeignKey(Resource, on_delete=models.CASCADE, verbose_name="资源")
    is_free = models.BooleanField('是否免费', default=False)

    class Meta:
        db_table = 'tb_chapters'
        verbose_name_plural = '章节'

    def __str__(self):
        return self.title


# 小节  TODO 与学习内容关联
class SectionsModel(models.Model):
    title = models.CharField("小节名", max_length=32)
    # sequence = models.SmallIntegerField("展示顺序")
    content = models.TextField("内容", max_length=10000)
    chapter = models.ForeignKey(ChaptersModel, on_delete=models.CASCADE, verbose_name="章节")

    class Meta:
        db_table = 'tb_sections'
        verbose_name_plural = '小节'

    def __str__(self):
        return self.title




# 学习任务
class LearnTaskModel(models.Model):
    remark = models.CharField('备注', max_length=128, null=True, blank=True)
    classs = models.ForeignKey(ClassModel, on_delete=models.CASCADE, verbose_name="班级")
    operator = models.ForeignKey(UserModel, on_delete=models.CASCADE, verbose_name="操作人/老师")

    class Meta:
        db_table = 'tb_lear_task'
        verbose_name_plural = '学习任务'

    def __str__(self):
        return self.remark



# 学习内容学习任务中间表
class Learning_TaskModel(models.Model):
    task = models.ForeignKey(LearnTaskModel,on_delete=models.CASCADE,verbose_name="所属任务")
    content = models.ForeignKey(Learning_content,on_delete=models.CASCADE,verbose_name="所有内容"  )

    class Meta:
        db_table = "resources_task_tb"
        verbose_name = "任务"
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.task



# 学习记录  TODO 艾宾浩斯记忆曲线
class LearnRecordModel(models.Model):
    user = models.ForeignKey(UserModel, on_delete=models.CASCADE, verbose_name="学习人")
    task = models.ForeignKey(LearnTaskModel, on_delete=models.CASCADE, verbose_name="任务")
    learn_time = models.IntegerField("学习时长(单位分钟)", default=0)
    level = models.SmallIntegerField('学习程度 0 生疏 1掌握', )

    class Meta:
        db_table = 'tb_lear_record'
        verbose_name_plural = '学习记录'

    def __str__(self):
        return self.user

# 收藏
class CollectModel(models.Model):
    user = models.ForeignKey(UserModel, on_delete=models.CASCADE, verbose_name="收藏人")
    content = models.ForeignKey(Learning_content, on_delete=models.CASCADE, verbose_name="收藏内容", null=True)
    is_collect = models.BooleanField(verbose_name="是否收藏", default=1)

    class Meta:
        db_table = "collect_tb"
        verbose_name = "收藏"
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.user.user_name


# 点赞评论
class LikeModel(models.Model):
    user = models.ForeignKey(UserModel, on_delete=models.CASCADE, verbose_name="点赞人")
    comment = models.ForeignKey(Comment, on_delete=models.CASCADE, verbose_name="评论id")

    class Meta:
        db_table = 'tb_like'
        verbose_name_plural = '点赞评论'

    def __str__(self):
        return self.comment


# 搜索记录
class SearchRecordModel(models.Model):
    user = models.ForeignKey(UserModel, on_delete=models.CASCADE, verbose_name="搜索人")
    content = models.CharField("搜索内容", max_length=56)

    class Meta:
        db_table = 'tb_search_record'
        verbose_name_plural = '搜索记录'

    def __str__(self):
        return self.content


# 消息类型表
class TypesModel(models.Model):
    tname = models.CharField("消息类型", max_length=32)

    class Meta:
        db_table = 'tb_types'
        verbose_name_plural = '消息类型表'

    def __str__(self):
        return self.tname


# 消息表
class MessageModel(models.Model):
    receive = models.ForeignKey(UserModel, on_delete=models.CASCADE, verbose_name="接收人")
    send = models.SmallIntegerField("发送人", null=True, blank=True)
    text = models.CharField("消息内容", max_length=532)
    types = models.ForeignKey(TypesModel, on_delete=models.CASCADE, verbose_name="消息类型")

    class Meta:
        db_table = 'tb_message'
        verbose_name_plural = '消息表'

    def __str__(self):
        return self.receive